# -*-encoding=utf-8-*- 
__author__ = '帆'

import urllib2, urllib, json, string

class CreateTags:

    apiUrl = 'http://api.map.baidu.com/place/v2/search'

    def __init__(self):
        pass
    def requestUrl(self, location, keywords):
        url = self.createUrl(location, keywords)
        print(url)
        request = urllib2.Request(url)
        request.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")

        try:
            responed = urllib2.urlopen(request)
            result = responed.read()
            result = result.decode('utf-8')
        except Exception,e:
            print(e.message)
            return None
        results = json.loads(result)
        results = results.get('results')
        return results

    def createUrl(self, location, keywords):
        # print(''.join(location))

        query = {
            'q': string.join(keywords, ','),
            'location': string.join(location, ','),
            'output': 'json',
            # 'scope': '1,2',
            # 'page_size': 20,
            'ak': 'GuD15I9XtUZhnglyMkZODKWq',
            'radius': 2000
        }
        query = urllib.urlencode(query)
        url = self.apiUrl+'?'+query
        # print(url)
        return url